using System;

namespace Multiplicity.Runtime.Serialization.Binary {

    public static class UnixTime {

        public static uint FromDateTime(DateTime dateTime) {
            Throw.ArgumentExceptionIf((dateTime < UnixEraBegin), "DateTime too small for unix time");
            Throw.ArgumentExceptionIf((dateTime > UnixEraEnd), "DateTime too big for unix time");

            return (uint)(dateTime - UnixEraBegin).TotalSeconds;
        }

        public static DateTime ToDateTime(uint unixTime) {
            return UnixEraBegin.AddSeconds(unixTime);
        }

        static readonly DateTime UnixEraBegin = new DateTime(1970, 1, 1);
        static readonly DateTime UnixEraEnd = UnixEraBegin.AddSeconds(uint.MaxValue);
    }
}
